cmake_minimum_required(VERSION 3.14)

# 设置工程名
project(JCANNX)

# --- 环境检查 ---             
set(JAVA_HOME "/usr/lib/jvm/java-11-openjdk-11.0.25.9-3.hce2.aarch64")
set(ASCEND_PATH "/usr/local/Ascend/ascend-toolkit/latest")

message("JNI路径 ${JAVA_HOME}/include")
message("Ascend路径 ${ASCEND_PATH}/include/")

# --- 基础设置 ---
add_compile_options(-std=c++11)

# 设置编译选项
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY  "./bin")    
set(CMAKE_CXX_FLAGS_DEBUG "-fPIC -O0 -g -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "-fPIC -O2 -Wall")

# 指定源文件所在目录
set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)

# --- 编译共享库 ---
set(SHARED_LIB_NAME "jcannx")

set(SHARED_LIB_SOURCES
  ${SOURCE_DIR}/common.cpp
  ${SOURCE_DIR}/matrix_mul.cpp
  ${SOURCE_DIR}/jcannx.cpp
)

add_library(${SHARED_LIB_NAME} SHARED ${SHARED_LIB_SOURCES})

set_target_properties(${SHARED_LIB_NAME} PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/build/bin"
    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/build/lib"
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/build/lib"
)

target_include_directories(${SHARED_LIB_NAME} PUBLIC 
  ${CMAKE_CURRENT_SOURCE_DIR}/include
  ${JAVA_HOME}/include
  ${JAVA_HOME}/include/linux
  ${ASCEND_PATH}/include
  ${ASCEND_PATH}/include/aclnn
)

target_link_libraries(${SHARED_LIB_NAME} PRIVATE
                      ${ASCEND_PATH}/lib64/libascendcl.so
                      ${ASCEND_PATH}/lib64/libnnopbase.so
                      ${ASCEND_PATH}/lib64/libopapi.so)

# --- 编译可执行文件 ---
set(EXECUTABLE_NAME "test")

set(EXECUTABLE_SOURCES
	${SOURCE_DIR}/common.cpp
	${SOURCE_DIR}/matrix_mul.cpp
	${SOURCE_DIR}/test.cpp
)

add_executable(${EXECUTABLE_NAME} ${EXECUTABLE_SOURCES})

set_target_properties(${EXECUTABLE_NAME} PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/build/bin"
)

target_include_directories(${EXECUTABLE_NAME} PUBLIC 
	${CMAKE_CURRENT_SOURCE_DIR}/include
  ${JAVA_HOME}/include
  ${JAVA_HOME}/include/linux
  ${ASCEND_PATH}/include
  ${ASCEND_PATH}/include/aclnn
)

target_link_libraries(${EXECUTABLE_NAME} PRIVATE
                      ${ASCEND_PATH}/lib64/libascendcl.so
                      ${ASCEND_PATH}/lib64/libnnopbase.so
                      ${ASCEND_PATH}/lib64/libopapi.so)



